package com.ljz.controller;

import com.ljz.dao.ProductRepository;
import com.ljz.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by jiazhang on 2017/4/13.
 */

@RestController

public class ProductController {


    @Autowired
    ProductRepository productRepository;

    /**
     * 查所所有信息
     * @return
     */
    @GetMapping(value = "/product")
    private List<Product>productList() {
        return productRepository.findAll();
    }


    /**
     * add message
     * @param pid
     * @param pname
     * @param pprice
     * @param premain
     * @param mid
     * @return
     */
    @PostMapping(value = "/product")
    public Product productAdd(@RequestParam("pid") Integer pid,
                              @RequestParam("pname") String pname,
                              @RequestParam("pprice") Double pprice,
                              @RequestParam("premain") Integer premain,
                              @RequestParam("mid") Integer mid) {

        Product product = new Product();
        product.setMid(mid);
        product.setPid(pid);
        product.setPname(pname);
        product.setPprice(pprice);
        product.setPremain(premain);

        return productRepository.save(product);
    }
}
